package com.totoro.mall.mall_buyer.service.impl;

import com.totoro.mall.mall_buyer.model.ProductCategory;
import com.totoro.mall.mall_buyer.repository.ProductCategoryRepository;
import com.totoro.mall.mall_buyer.service.IProductCategoryService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Niki on 2018/4/3 9:22
 */
@Service
public class ProductCategoryServiceImpl implements IProductCategoryService {
    @Resource
    ProductCategoryRepository productCategoryRepository ;
    @Override
    public Integer save(ProductCategory productCategory) {
        productCategory = productCategoryRepository.save(productCategory);
        return productCategory.getCategoryId();
    }

    @Override
    public void update(ProductCategory productCategory) {
        productCategoryRepository.save(productCategory);
    }

    @Override
    public ProductCategory findById(Integer categoryId) {
        return productCategoryRepository.findById(categoryId).get();
    }

    @Override
    public List<ProductCategory> findAll() {
        return productCategoryRepository.findAll();
    }

    @Override
    public Page<ProductCategory> findByPage(int page, int size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        return productCategoryRepository.findAll(pageRequest);
    }
}
